package com.three.iot.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.three.iot.api.DeviceService;
import com.three.iot.api.UserDeviceService;
import com.three.iot.api.UserService;
import com.three.iot.channel.api.DeviceChannelService;
import com.three.iot.channel.po.*;
import com.three.iot.commons.constant.CommonConstants;
import com.three.iot.commons.dto.*;
import com.three.iot.commons.enums.*;
import com.three.iot.commons.exceptions.BusinessException;
import com.three.iot.commons.utils.DateUtil;
import com.three.iot.jdbc.dao.DictUtils;
import com.three.iot.jdbc.mapper.SysUserMapper;
import com.three.iot.jdbc.mapper.TDeviceInfoMapper;
import com.three.iot.jdbc.mapper.TProductAttrMapper;
import com.three.iot.jdbc.mapper.TUserDeviceMapper;
import com.three.iot.jdbc.model.SysUser;
import com.three.iot.jdbc.model.TDeviceInfo;
import com.three.iot.jdbc.model.TProductAttr;
import com.three.iot.jdbc.model.TUserDevice;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

/**
 * @author luiz
 * @Title: UserDeviceImpl
 * @ProjectName iot-server
 * @Description: TODO
 * @date 2019-05-23 10:00
 */
@Service
@Slf4j
public class UserDeviceServiceImpl implements UserDeviceService {
    @Resource
    private TDeviceInfoMapper tDeviceInfoMapper;
    @Resource
    private TUserDeviceMapper tUserDeviceMapper;
    @Resource
    private TProductAttrMapper tProductAttrMapper;
    @Resource
    private DeviceChannelService deviceChannelService;
    @Resource
    private UserService userService;
    @Resource
    private SysUserMapper sysUserMapper;
    @Resource
    private DeviceService deviceService;

    /**
     * 根据用户编号获取设备列表
     * 再根据设备编号查询设备状态
     * @param userNo
     * @return
     */
    @Override
    public Result<List<UserDeviceRespDto>> findDeviceList(String userNo) {
        List<TDeviceInfo> tDeviceInfoList=findDeviceInfoList(userNo);
        if(tDeviceInfoList==null||tDeviceInfoList.size()<=0){
            return Result.newError(ResultCode.COMMON_DATA_EXISTS);
        }
        List<UserDeviceRespDto>  userDeviceRespDtos=new ArrayList<>();
        for(TDeviceInfo tDeviceInfo:tDeviceInfoList){
            UserDeviceRespDto userDeviceRespDto=new UserDeviceRespDto();
            userDeviceRespDto.setBindStatus(BindStatusEnum.BIND.getCode());
            userDeviceRespDto.setBindStatusName(BindStatusEnum.BIND.getName());
            convertDto(userDeviceRespDto,tDeviceInfo);
            Result<DeviceDetailRespPo> respPoResult=deviceChannelService.findDeviceByNo(tDeviceInfo.getIotId());
            if(respPoResult.isSuccess()){
                DeviceDetailRespPo deviceDetailRespPo=respPoResult.getData();
                userDeviceRespDto.setDeviceStatus(deviceDetailRespPo.getDeviceStatus());
                userDeviceRespDto.setDeviceStatusName(deviceDetailRespPo.getDeviceStatusName());
                userDeviceRespDto.setAddTime(deviceDetailRespPo.getGmtCreate());
                userDeviceRespDto.setActiveTime(deviceDetailRespPo.getGmtActive());
                userDeviceRespDto.setLastOnlineTime(deviceDetailRespPo.getGmtOnline());
            }
            userDeviceRespDtos.add(userDeviceRespDto);
        }
        return Result.newSuccess(userDeviceRespDtos);
    }

    @Override
    public Result<List<UserDeviceRespDto>> findDbDeviceList(String userNo) {
        List<TDeviceInfo> tDeviceInfoList=findDeviceInfoList(userNo);
        if(tDeviceInfoList==null||tDeviceInfoList.size()<=0){
            return Result.newError(ResultCode.COMMON_DATA_EXISTS);
        }
        List<UserDeviceRespDto>  userDeviceRespDtos=new ArrayList<>();
        for(TDeviceInfo tDeviceInfo:tDeviceInfoList){
            UserDeviceRespDto userDeviceRespDto=new UserDeviceRespDto();
            userDeviceRespDto.setBindStatus(BindStatusEnum.BIND.getCode());
            userDeviceRespDto.setBindStatusName(BindStatusEnum.BIND.getName());
            convertDto(userDeviceRespDto,tDeviceInfo);
            userDeviceRespDtos.add(userDeviceRespDto);
        }
        return Result.newSuccess(userDeviceRespDtos);
    }

    private List<TDeviceInfo> findDeviceInfoList(String userNo) {
        List<TUserDevice> tUserDevices=tUserDeviceMapper.selectByUserNo(userNo);
        if(tUserDevices==null || tUserDevices.size()==0){
            return null;
        }
        List<String> iotIdLists=new ArrayList<String>();
        for(TUserDevice tUserDevice:tUserDevices){
            iotIdLists.add(tUserDevice.getIotId());
        }
        List<TDeviceInfo> tDeviceInfoList=tDeviceInfoMapper.selectByIotIds(iotIdLists);
        return tDeviceInfoList;
    }

    private void convertDto(UserDeviceRespDto userDeviceRespDto, TDeviceInfo tDeviceInfo) {
        userDeviceRespDto.setDeviceId(tDeviceInfo.getDeviceId());
        userDeviceRespDto.setIotId(tDeviceInfo.getIotId());
        userDeviceRespDto.setDeviceName(tDeviceInfo.getDeviceName());
        userDeviceRespDto.setDeviceNickName(tDeviceInfo.getDeviceNickName());
        userDeviceRespDto.setDeviceStatus(IotDeviceStatusEnum.OFFLINE.getCode());
        userDeviceRespDto.setDeviceStatusName(IotDeviceStatusEnum.OFFLINE.getName());
        userDeviceRespDto.setAddTime(DateUtil.getDateTimeFormat(tDeviceInfo.getAddTime()));
        userDeviceRespDto.setActiveTime(DateUtil.getDateTimeFormat(tDeviceInfo.getActiveTime()));
        userDeviceRespDto.setLastOnlineTime(DateUtil.getDateTimeFormat(tDeviceInfo.getLastOnlineTime()));
    }


    /**
     * 查询设备属性数据
     * @param iotId
     * @param  groupNo
     * @param userNo
     * @return
     */
    @Override
    public Result<DeviceMultiAttrDataRespDto> findMultiAttrDataList(String iotId, String groupNo , String userNo) {
        TDeviceInfo tDeviceInfo=tDeviceInfoMapper.selectByIotId(iotId);
        if(tDeviceInfo==null){
            return Result.newError(ResultCode.DEVICE_NOT_EXISTS);
        }
        List<TProductAttr> tProductAttrs=tProductAttrMapper.selectByProductKeyAndGroupNo(tDeviceInfo.getProductKey(),groupNo);
        List<String> attrLists=new ArrayList<String>();
        //阿里云批量查询每次只能查询10个
        for(TProductAttr tProductAttr:tProductAttrs){
            if(tProductAttr.getStatus()==StatusEnum.VALID.getCode()){
                attrLists.add(tProductAttr.getAttrNo());
            }
        }
        /**
         * 封装设备属性值查询条件
         */
        DevicePropertysDataPo devicePropertysDataPo=new DevicePropertysDataPo();
        devicePropertysDataPo.setIotId(iotId);
        devicePropertysDataPo.setDeviceName(tDeviceInfo.getDeviceName());
        devicePropertysDataPo.setProductKey(tDeviceInfo.getProductKey());
        devicePropertysDataPo.setIdentifiers(attrLists);
        //默认往前查1天
        Long startTime=System.currentTimeMillis()- CommonConstants.ONE_DAY_MILSECONDS;
        Long endTime=System.currentTimeMillis();
        devicePropertysDataPo.setStartTime(startTime);
        devicePropertysDataPo.setEndTime(endTime);
        //默认条数 50
        devicePropertysDataPo.setPageSize(CommonConstants.MAX_PAGE_SIZE);
        //默认正序
        devicePropertysDataPo.setAsc(CommonConstants.DEFAULT_PAGE_ASC);
        Result<DeviceMultiPropertyDataRespPo>  deviceMultiPropertyDataRespPoResult=deviceChannelService.findDevicePropertysData(devicePropertysDataPo);
        /**
         * 封装设备属性数据list
         */
        DeviceMultiAttrDataRespDto deviceMultiAttrDataRespDto=buildRespDto(deviceMultiPropertyDataRespPoResult);
        return Result.newSuccess(deviceMultiAttrDataRespDto);
    }

    private DeviceMultiAttrDataRespDto buildRespDto(Result<DeviceMultiPropertyDataRespPo> deviceMultiPropertyDataRespPoResult) {
        if(!deviceMultiPropertyDataRespPoResult.isSuccess()){
            return null;
        }

        DeviceMultiPropertyDataRespPo deviceMultiPropertyDataRespPo=deviceMultiPropertyDataRespPoResult.getData();
        DeviceMultiAttrDataRespDto deviceMultiAttrDataRespDto=new DeviceMultiAttrDataRespDto();
        deviceMultiAttrDataRespDto.setIotId(deviceMultiPropertyDataRespPo.getIotId());
        deviceMultiAttrDataRespDto.setNextTime(deviceMultiPropertyDataRespPo.getNextTime());
        deviceMultiAttrDataRespDto.setNextValid(deviceMultiPropertyDataRespPo.getNextValid());
        List<PropertyDataRespPo> propertyDataRespPos=deviceMultiPropertyDataRespPo.getPropertyRespPoList();
        List<DeviceAttrRespDto> deviceAttrRespDtos=new ArrayList<>();
        //获取设备所有属性
        Result<List<ProductAttrRespDto>> tProductAttrResult=deviceService.findAttrByIotId(deviceMultiPropertyDataRespPo.getIotId());
        List<ProductAttrRespDto> productAttrRespDtoList=tProductAttrResult.getData();
        for(PropertyDataRespPo propertyDataRespPo:propertyDataRespPos){
            ProductAttrRespDto productAttrRespDto = matchDeviceAttr(productAttrRespDtoList,propertyDataRespPo.getIdentifier());
            DeviceAttrRespDto deviceAttrRespDto=buildDeviceAttr(productAttrRespDto);
            List<DeviceAttrDataDetailRespDto> deviceAttrDataDetailRespDtos=buildDeviceAttrDataDetail(propertyDataRespPo);
            deviceAttrRespDto.setDeviceAttrDataDetailRespDtos(deviceAttrDataDetailRespDtos);
            if(deviceAttrDataDetailRespDtos.size()>=1){
                int lastIndex=0;
                DeviceAttrDataDetailRespDto deviceAttrDataDetailRespDto=deviceAttrDataDetailRespDtos.get(lastIndex);
                //获取当前最新值  倒序
                deviceAttrRespDto.setCurrentVal(deviceAttrDataDetailRespDto.getValue());
            }
            deviceAttrRespDtos.add(deviceAttrRespDto);
        }
        deviceAttrRespDtos.sort(new Comparator<DeviceAttrRespDto>() {
            @Override
            public int compare(DeviceAttrRespDto o1, DeviceAttrRespDto o2) {
                //按照属性编号分组 方便展示
                /* int retNum = o1.getAttrNo().compareToIgnoreCase(o2.getAttrNo());
                 //特殊开关，优先靠前
                if(specialAttrNo.contains(o1.getAttrNo())){
                    retNum=-1;
                }
                if(specialAttrNo.contains(o2.getAttrNo())){
                    retNum=1;
                }
                return retNum;*/
                //按照序号排序
                return o1.getSortNum()-o2.getSortNum();
            }
        });
        deviceMultiAttrDataRespDto.setDeviceAttrRespDtos(deviceAttrRespDtos);
        return  deviceMultiAttrDataRespDto;
    }

    private List<DeviceAttrDataDetailRespDto> buildDeviceAttrDataDetail(PropertyDataRespPo propertyDataRespPo) {
        List<DeviceAttrDataDetailRespDto> deviceAttrDataDetailRespDtos=new ArrayList<DeviceAttrDataDetailRespDto>();
        for(PropertyDataDetailRespPo propertyDataDetailRespPo:propertyDataRespPo.getPropertyDataDetailRespPoList()){
            DeviceAttrDataDetailRespDto deviceAttrDataDetailRespDto=new DeviceAttrDataDetailRespDto();
            deviceAttrDataDetailRespDto.setTime(propertyDataDetailRespPo.getTime());
            deviceAttrDataDetailRespDto.setValue(propertyDataDetailRespPo.getValue());
            deviceAttrDataDetailRespDtos.add(deviceAttrDataDetailRespDto);
        }
        return deviceAttrDataDetailRespDtos;
    }

    private ProductAttrRespDto matchDeviceAttr(List<ProductAttrRespDto> productAttrRespDtoList, String identifier) {
        for(ProductAttrRespDto productAttrRespDto:productAttrRespDtoList){
            if(productAttrRespDto.getAttrNo().equals(identifier)){
                return productAttrRespDto;
            }
        }
        return null;
    }


    @Override
    public Result setDeviceProperty(DevicePropertyControlReqDto devicePropertyControlReqDto) {
        ProductAttrRespDto productAttrRespDto=deviceService.findAttrByIotIdAndAttrNo(devicePropertyControlReqDto.getIotId(),devicePropertyControlReqDto.getAttrNo());
        if(productAttrRespDto==null){
            throw new BusinessException(ResultCode.ATTR_NOT_EXISTS);
        }

        DevicePropertyPo devicePropertyPo=new DevicePropertyPo();
        devicePropertyPo.setIotId(devicePropertyControlReqDto.getIotId());
        JSONObject paramJson=new JSONObject();
        paramJson.put(devicePropertyControlReqDto.getAttrNo(),new BigDecimal(devicePropertyControlReqDto.getAttrVal()));
        if(DataTypeEnum.INT32.getCode().equals(productAttrRespDto.getAttrDataType())){
            paramJson.put(devicePropertyControlReqDto.getAttrNo(),Integer.valueOf(devicePropertyControlReqDto.getAttrVal()));
        }else if(DataTypeEnum.BOOL.getCode().equals(productAttrRespDto.getAttrDataType())){
            paramJson.put(devicePropertyControlReqDto.getAttrNo(),Integer.valueOf(devicePropertyControlReqDto.getAttrVal()));
        }
        devicePropertyPo.setParamObj(paramJson);
        return deviceChannelService.setDeviceProperty(devicePropertyPo);
    }

    /**
     * 按照设备属性查询数据列表
     * @param devicePropertyDataPo
     * @return
     */
    private DeviceSingleAttrDataRespDto findSingleAttrList(DevicePropertyDataPo devicePropertyDataPo, ProductAttrRespDto productAttrRespDto){
        DeviceSingleAttrDataRespDto deviceAttrDataRespDto=new DeviceSingleAttrDataRespDto();
        /**
         * 封装设备属性
         */
        DeviceAttrRespDto deviceAttrRespDto=buildDeviceAttr(productAttrRespDto);
        Result<DeviceSinglePropertyDataRespPo> devicePropertyDataRespPoResult=deviceChannelService.findDevicePropertyData(devicePropertyDataPo);
        if(devicePropertyDataRespPoResult.isSuccess()){
            DeviceSinglePropertyDataRespPo deviceSinglePropertyDataRespPo = devicePropertyDataRespPoResult.getData();
            deviceAttrDataRespDto.setIotId(deviceSinglePropertyDataRespPo.getIotId());
            deviceAttrDataRespDto.setNextTime(deviceSinglePropertyDataRespPo.getNextTime());
            deviceAttrDataRespDto.setNextValid(deviceSinglePropertyDataRespPo.getNextValid());
            PropertyDataRespPo propertyDataRespPo = deviceSinglePropertyDataRespPo.getPropertyDataRespPo();
            List<DeviceAttrDataDetailRespDto> deviceAttrDataDetailRespDtos=buildDeviceAttrDataDetail(propertyDataRespPo);
            deviceAttrRespDto.setDeviceAttrDataDetailRespDtos(deviceAttrDataDetailRespDtos);
            if(deviceAttrDataDetailRespDtos.size()>=1){
                int lastIndex=0;
                if(devicePropertyDataPo.getAsc()==1){
                    lastIndex=deviceAttrDataDetailRespDtos.size()-1;
                }
                DeviceAttrDataDetailRespDto deviceAttrDataDetailRespDto=deviceAttrDataDetailRespDtos.get(lastIndex);
                //获取当前最新值 取最后一条
                deviceAttrRespDto.setCurrentVal(deviceAttrDataDetailRespDto.getValue());
            }
        }

        deviceAttrDataRespDto.setDeviceAttrRespDto(deviceAttrRespDto);
        return deviceAttrDataRespDto;
    }

    private DeviceAttrRespDto buildDeviceAttr(ProductAttrRespDto productAttrRespDto) {
        if(productAttrRespDto==null){
            log.error("查询数据出错了.....");
            throw new BusinessException(ResultCode.FAIL);
        }
        DeviceAttrRespDto deviceAttrRespDto=new DeviceAttrRespDto();
        BeanUtils.copyProperties(productAttrRespDto,deviceAttrRespDto);
        deviceAttrRespDto.setProductAttrId(productAttrRespDto.getId());
        return deviceAttrRespDto;
    }

    @Override
    public Result findSigleAttrDataList(DevicePropertyDataReqDto devicePropertyDataReqDto) {
        ProductAttrRespDto productAttrRespDto=deviceService.findAttrByIotIdAndAttrNo(devicePropertyDataReqDto.getIotId(),devicePropertyDataReqDto.getIdentifier());
        if(productAttrRespDto==null){
            return Result.newError(ResultCode.ATTR_NOT_EXISTS);
        }
        /**
         * 封装设备属性请求参数
         */
        DevicePropertyDataPo devicePropertyDataPo=new DevicePropertyDataPo();
        BeanUtils.copyProperties(devicePropertyDataReqDto,devicePropertyDataPo);
        /**
         * 封装设备属性数据list
         */
        DeviceSingleAttrDataRespDto deviceAttrDataRespDto=findSingleAttrList(devicePropertyDataPo,productAttrRespDto);
        return Result.newSuccess(deviceAttrDataRespDto);
    }

    @Override
    public Result<List<UserResultDto>> findUserByIotId(String iotId) {
        List<TUserDevice> tUserDevices = tUserDeviceMapper.selectByIotId(iotId);
        List<UserResultDto> userResultDtos=new ArrayList<>();
        for(TUserDevice tUserDevice:tUserDevices){
            String userNo=tUserDevice.getUserNo();
            Result<UserResultDto> userResultDto=userService.findByUniqIndex(userNo);
            if(userResultDto.isSuccess()){
                userResultDtos.add(userResultDto.getData());
            }
        }
        return Result.newSuccess(userResultDtos);
    }

    @Override
    public Result bindUser(String iotId, String userNo,String operUserNo) {
        TUserDevice tUserDevice=tUserDeviceMapper.selectByUniqIndex(iotId,userNo);
        if(tUserDevice!=null){
            tUserDevice.setStatus(StatusEnum.VALID.getCode());
            tUserDevice.setModiNo(operUserNo);
            tUserDevice.setModiTime(new Date());
            tUserDeviceMapper.updateByPrimaryKeySelective(tUserDevice);
        }else {
            tUserDevice=new TUserDevice();
            tUserDevice.setUserNo(userNo);
            tUserDevice.setIotId(iotId);
            tUserDevice.setStatus(StatusEnum.VALID.getCode());
            tUserDevice.setCreateNo(operUserNo);
            tUserDevice.setCreateTime(new Date());
            tUserDevice.setModiNo(operUserNo);
            tUserDevice.setModiTime(new Date());
            tUserDeviceMapper.insertSelective(tUserDevice);
        }
        return Result.newSuccess();
    }

    @Override
    public Result unbind(String iotId, String userNo, String operUserNo) {
        TUserDevice tUserDevice=tUserDeviceMapper.selectByUniqIndex(iotId,userNo);
        if(tUserDevice!=null){
            tUserDevice.setStatus(StatusEnum.UNVALID.getCode());
            tUserDevice.setModiNo(operUserNo);
            tUserDevice.setModiTime(new Date());
            tUserDeviceMapper.updateByPrimaryKeySelective(tUserDevice);
        }
        return Result.newSuccess();
    }

    @Override
    public Result findDeviceByUserNo(String userNo) {
        //先按照用户主键ID查询
        if(StringUtils.isNumeric(userNo)){
            SysUser sysUser=sysUserMapper.selectByPrimaryKey(Integer.valueOf(userNo));;
            if(sysUser!=null){
                userNo=sysUser.getUserNo();
            }
        }
        List<TUserDevice> tUserDevices=tUserDeviceMapper.selectByUserNo(userNo);
        List<TDeviceInfo> tDeviceInfoList=tDeviceInfoMapper.selectAll();
        List<UserDeviceRespDto>  userDeviceRespDtos=new ArrayList<>();
        for(TDeviceInfo tDeviceInfo:tDeviceInfoList){
            UserDeviceRespDto userDeviceRespDto=new UserDeviceRespDto();
            userDeviceRespDto.setBindStatus(BindStatusEnum.UNBIND.getCode());
            userDeviceRespDto.setBindStatusName(BindStatusEnum.UNBIND.getName());
            convertDto(userDeviceRespDto,tDeviceInfo);
            processBindStatus(tUserDevices,userDeviceRespDto);
            userDeviceRespDtos.add(userDeviceRespDto);
        }
        return Result.newSuccess(userDeviceRespDtos);
    }
    private void processBindStatus(List<TUserDevice> tUserDevices, UserDeviceRespDto userDeviceRespDto) {
        for(TUserDevice tUserDevice:tUserDevices){
            if(userDeviceRespDto.getIotId().equals(tUserDevice.getIotId())){
                userDeviceRespDto.setBindStatus(BindStatusEnum.BIND.getCode());
                userDeviceRespDto.setBindStatusName(BindStatusEnum.BIND.getName());
            }
        }
    }

    @Override
    public Result findUserByDeviceId(String iotId) {
        List<SysUser> sysUsers=sysUserMapper.selectAll();
        List<TUserDevice> tUserDevices=tUserDeviceMapper.selectByIotId(iotId);
        List<UserResultDto> userResultDtos=new ArrayList<>();
        for(SysUser sysUser:sysUsers){
            UserResultDto userResultDto=new UserResultDto();
            userResultDto.setBindStatus(BindStatusEnum.UNBIND.getCode());
            userResultDto.setBindStatusName(BindStatusEnum.UNBIND.getName());
            userResultDto.setId(sysUser.getId().toString());
            userResultDto.setCreateTime(DateUtil.getDateTimeFormat(sysUser.getCreateTime()));
            userResultDto.setModiTime(DateUtil.getDateTimeFormat(sysUser.getModiTime()));
            userResultDto.setStatus(sysUser.getStatus().toString());
            userResultDto.setStatusName(StatusEnum.getNameByCode(sysUser.getStatus()));
            userResultDto.setIfAdmin(String.valueOf(sysUser.getIfAdmin()));
            userResultDto.setIfAdminName(IfAdminEnum.getNameByCode(sysUser.getIfAdmin()));
            BeanUtils.copyProperties(sysUser,userResultDto);
            bindStatusProcess(userResultDto,tUserDevices);
            userResultDtos.add(userResultDto);
        }
        return Result.newSuccess(userResultDtos);
    }

    @Override
    public Result bindDeviceList(List<UserDeviceDto> userDeviceDtos) {
        List<TUserDevice> userDevices=new ArrayList<>();
        for(UserDeviceDto userDeviceDto:userDeviceDtos){
            TUserDevice tUserDevice=new TUserDevice();
            BeanUtils.copyProperties(userDeviceDto,tUserDevice);
            userDevices.add(tUserDevice);
        }
        tUserDeviceMapper.insertBatch(userDevices);
        return Result.newSuccess();
    }

    @Override
    public Result bindUserDeviceList(String userNo, List<String> iotList) {
        if(iotList==null || iotList.size()<1){
            return Result.newSuccess();
        }
        tUserDeviceMapper.deleteByUserNo(userNo);
        List<TUserDevice> userDevices=new ArrayList<>();
        for(String iotId:iotList){
            TUserDevice tUserDevice=new TUserDevice();
            tUserDevice.setUserNo(userNo);
            tUserDevice.setIotId(iotId);
            userDevices.add(tUserDevice);
        }
        tUserDeviceMapper.insertBatch(userDevices);
        return Result.newSuccess();
    }

    @Override
    public Result bindDeviceUserList(String iotId, List<String> userList) {
        tUserDeviceMapper.deleteByIotId(iotId);
        List<TUserDevice> userDevices=new ArrayList<>();
        for(String userNo:userList){
            TUserDevice tUserDevice=new TUserDevice();
            tUserDevice.setUserNo(userNo);
            tUserDevice.setIotId(iotId);
            userDevices.add(tUserDevice);
        }
        tUserDeviceMapper.insertBatch(userDevices);
        return Result.newSuccess();
    }



    private void bindStatusProcess(UserResultDto userResultDto, List<TUserDevice> tUserDevices) {
        for(TUserDevice tUserDevice:tUserDevices){
            if(userResultDto.getUserNo().equals(tUserDevice.getUserNo())){
                userResultDto.setBindStatus(BindStatusEnum.BIND.getCode());
                userResultDto.setBindStatusName(BindStatusEnum.BIND.getName());
            }
        }
    }

}
